import { _decorator, Collider2D, Component, Contact2DType, EventTouch, IPhysics2DContact, Label, Node, TERRAIN_SOUTH_INDEX } from 'cc';
import { BULLET, Constant, WEAPON } from '../AshFramework/Datas/Constant';
import { GameData } from './GameData';
import { PrefsManager } from '../AshFramework/Managers/PrefsManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
import { TLWLSJ_UIManager } from './UIManager';
import { TipsController } from './TipsController';
const { ccclass, property } = _decorator;

@ccclass('Prop')
export class Prop extends Component {

    Collider2D: Collider2D = null;
    Button: Node = null;

    protected onLoad(): void {
        this.Collider2D = this.getComponent(Collider2D);
        this.Button = this.node.children[0];
        if (this.Collider2D) {
            this.Collider2D.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
            this.Collider2D.on(Contact2DType.END_CONTACT, this.onEndContact, this);
        }
    }


    protected start(): void {
        if (this.node.name === "枪") {
            TipsController.Instance.IsPack = true;
        }
        TipsController.Instance.NeedTips++;
    }

    ButtonClick(event: EventTouch) {
        const target: Node = event.getCurrentTarget();
        AudioManager.PlaySound(Audios.Get);
        switch (target.name) {
            case "枪":
                TLWLSJ_UIManager.Instance.getZDSQ();
                break;
            case "子弹":
                GameData.addBulletByType(BULLET.软铅弹头9x19mm, 30);
                break;
        }
        this.node.destroy();
        TipsController.Instance.NeedTips--;
        TipsController.Instance.check();
    }


    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        if (otherCollider.group == Constant.Group.PLAYER) {
            this.Button.active = true;
            TipsController.Instance.addTarget(this.node);
        }
    }

    onEndContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
        if (otherCollider.group == Constant.Group.PLAYER) {
            this.Button.active = false;
            TipsController.Instance.removeTarget(this.node);
        }
    }
}


